<?
class CUser
{
	function Add($arFields)
	{
		global $DB;
		
		// Сделать проверку на существование логина в базе
		// ...
		
		$arFields = Array(
		  "LOGIN"             => MySQL::SQLValue($arFields['LOGIN']),
		  "EMAIL"             => MySQL::SQLValue($arFields['EMAIL']),
		  "ACTIVE"            => MySQL::SQLValue("Y"),
		  "PASSWORD"          => MySQL::SQLValue($arFields['PASSWORD']),
		  "DATE_REGISTER"     => MySQL::SQLValue("now", MySQL::SQLVALUE_DATETIME)
		);
		
		if($id = $DB->InsertRow("b_user", $arFields))
		{
			return $id;
		}
		else
		{
			//echo $DB->Error();
			$DB->Kill();
			return false;
		}
	}
	
	function GetByLogin($sLogin)
	{
		if(!is_string($sLogin)) return false;
		
		$arUser = self::GetList(false, array('LOGIN'=>$sLogin));
		
		//print_r('<pre>');print_r($arUser);print_r('</pre>');
		
		if($arUser) return $arUser;
		else return false;
	}
	
	function GetList($arSort = NULL, $arFilter = NULL, $arNav = NULL, $arSelect = NULL)
	{
		global $DB;
		
		$arSqlWhere = array();
		$WHERE = 'WHERE 1 = 1';
		
		if(is_array($arFilter) && count($arFilter))
		{
			foreach ($arFilter as $FIELD_NAME=>$FIELD_VALUE)
			{
				$OPERATOR = '=';

				if($FIELD_NAME && $FIELD_VALUE)
				{
					
					$LOGIC = 'AND';
					$WHERE .= ' '.$LOGIC.' '.$FIELD_NAME." ".$OPERATOR." ".MySQL::SQLValue($FIELD_VALUE);
				}
			}
		}
		
		$sql = "SELECT * FROM b_user ".$WHERE;
		//print_r($sql);
		//return;
		
		if(!$DB->Query($sql))
		{
			//echo $DB->Error();
			$DB->Kill();
		}
		
		while($arUser = $DB->RowArray(null, MYSQL_ASSOC))
		{
		    //print_r('<pre>');print_r($arUser);print_r('</pre>');
		    
		    return $arUser;
		}
		return false;
	}
	
	function IsAuthorized()
	{
		return ($_SESSION["SESS_AUTH"]["AUTHORIZED"] == "Y");
	}
	
	function Authorize()
	{
		$_SESSION["SESS_AUTH"]["AUTHORIZED"] = "Y";
	}
	
	function Logout()
	{
		unset($_SESSION["SESS_AUTH"]);
	}
	
	function CheckLogout()
	{
		if($_REQUEST['logout'] == 'Y') CUser::Logout();
	}
}